define(['jquery', 'bootstrap', 'backend', 'form', 'table', 'tinymce', 'layer'], function ($, undefined, Backend, Form, Table, tinymce, layer) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'expert/marketing_director_weighting_score_detail/index',
                    table: 'user_weighting_score_detail'
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'id',
                columns: [
                    { field: 'id', title: "ID", searchable: false, search: false },
                    { field: 'user', title: "用户", searchable: false, formatter: Controller.api.formatter.user, },
                    { field: 'u.nickname', title: "昵称", visible: false, operate: 'LIKE' },
                    { field: 'u.mobile', title: "手机号", visible: false, operate: 'LIKE' },
                    { field: 'level', title: "分红级别", searchable: false, formatter: Controller.api.formatter.level, },
                    { field: 'type', title: "分红类型", searchable: false, formatter: Controller.api.formatter.type, },
                    { field: 'add_time', title: "创建时间", searchable: false, formatter: Table.api.formatter.datetime, },
                    { field: 'status', title: "分红状态", searchable: false, formatter: Controller.api.formatter.status, },
                    { field: 'percent', title: "分红比例", searchable: false, formatter: Controller.api.formatter.percent, },
                    { field: 'month', title: "分红月份", searchList: Config.monthList, formatter: Controller.api.formatter.month, },
                    { field: 'score', title: "分红", searchable: false, formatter: Controller.api.formatter.score, },
                    { field: 'update_time', title: "分红时间", searchable: false, formatter: Table.api.formatter.datetime, },
                ],
                queryParams: function (params) { //自定义搜索条件
                    var filter = params.filter ? JSON.parse(params.filter) : {}; //判断当前是否还有其他高级搜索栏的条件
                    var op = params.op ? JSON.parse(params.op) : {};  //并将搜索过滤器 转为对象方便我们追加条件
                    var month = Fast.api.query("month");
                    if (month) {
                        filter.month = month;
                        op.month = "=";  //user_id的操作方法的为 找到相等的
                        params.filter = JSON.stringify(filter); //将搜索过滤器和操作方法 都转为JSON字符串
                        params.op = JSON.stringify(op);
                    }
                    //如果希望忽略搜索栏搜索条件,可使用
                    return params;
                },
                // commonSearch: false,
                visible: false,
                showToggle: false,
                showColumns: false,
                showExport: false,
                pageList: [10, 25, 50, 100, 'All']
            });

            //给添加按钮添加`data-area`属性
            $(".btn-add").data("area", ["80%", "100%"]);

            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone,.btn-edit").data("area", ["80%", "100%"]);
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            //加载当月情况
            if (Config.month != "") {
                Controller.loadMonthInfo(Config.month);
            }
            //选择月份
            $(".month-list").change(function () {
                var month = $(this).val();
                window.location.href = "?month=" + month;
            });

            //生成记录
            $(".btn-create-record").click(function () {

                var month = $(".month-list").val();
                if (month == "") {
                    layer.msg("请先选择月份");
                    return false;
                }
                var params = {
                    "month": month,
                };
                console.log("params", params);

                layer.confirm('是否确认生成记录?<br/>提示：如果之前有这个月份的记录,会覆盖', { icon: 3, title: __('Warning'), },
                    function (index) {

                        layer.close(index);

                        Fast.api.ajax({
                            url: 'expert/marketing_director_weighting_score_detail/create_record',
                            data: params,
                            success: function (data) {
                                console.log(data);
                                // layer.msg(data.msg);

                                layer.closeAll();

                                layer.msg(data.msg, { time: 1000 }, function () {
                                    if (data.code == 1) {
                                        window.location.href = "?month=" + month;
                                    }
                                });
                            }
                        });

                    });
            })

          
            //开始分红
            $(".btn-start-score").click(function () {

                var month = $(".month-list").val();
                if (month == "") {
                    layer.msg("请先选择月份");
                    return false;
                }

                var total_score = $("#total_score").val();
                if (total_score == "") {
                    layer.msg("请输入分红总额");
                    return false;
                }

                var params = {
                    "total_score": total_score,
                    "month": month,
                };
                console.log("params", params);

                layer.confirm('是否确认分红?', { icon: 3, title: __('Warning'), },
                    function (index) {

                        layer.close(index);

                        Fast.api.ajax({
                            url: 'expert/marketing_director_weighting_score_detail/start_score',
                            data: params,
                            success: function (data) {
                                console.log(data);
                                // layer.msg(data.msg);

                                layer.closeAll();

                                layer.msg(data.msg, { time: 1000 }, function () {
                                    if (data.code == 1) {
                                        window.location.href = "?month=" + month;
                                    }
                                });
                            }
                        });

                    });
            });

        },
        //加载当月情况
        loadMonthInfo: function (month) {
            var params = {
                "month": month,
            };
            Fast.api.ajax({
                url: 'expert/marketing_director_weighting_score_detail/query_month_score',
                data: params,
                success: function (json) {
                    console.log(json);
                    layer.closeAll();
                    //已经分红
                    if (json.code == 1) {
                        $(".btn-month-score").html("（ " + json.data.month + " ） 当月已分红，分红总额：" + json.data.total);
                        $(".btn-month-score").removeClass("btn-success");
                        $(".btn-month-score").addClass("btn-danger");
                    } else {
                        $(".btn-month-score").html("（ " + json.data.month + " ） 当月未分红");
                        $(".btn-month-score").removeClass("btn-danger");
                        $(".btn-month-score").addClass("btn-success");
                    }
                    // layer.msg(data.msg);
                }
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                thumb: function (value, row, index) {
                    var html = '';
                    if (value.indexOf("jpg") > -1 || value.indexOf("jpeg") > -1 || value.indexOf("png") > -1 || value.indexOf("gif") > -1) {
                        html = '<a href="' + row.cover + '" target="_blank"><img src="' + row.cover + '" alt="" style="max-height:60px;max-width:120px"></a>';
                    }
                    return '<div style="width:120px;margin:0 auto;text-align:center;overflow:hidden;white-space: nowrap;text-overflow: ellipsis;">' + html + '</div>';
                },
                yes_or_no: function (value, row, index) {
                    var html = "";
                    if (value == "1") {
                        html = "是";
                    } else {
                        html = "否";
                    }
                    return html;
                },
                user: function (value, row, index) {
                    var html = row["nickname"] + "<br/>" + row["mobile"];
                    return html;
                },
                level: function (value, row, index) {
                    var html = "";
                    // 分红级别：1三星达人，2一星董事，3二星董事，4三星董事，5区经理人
                    if (value == "1") {
                        html = "三星达人";
                    }
                    if (value == "2") {
                        html = "一星董事";
                    }
                    if (value == "3") {
                        html = "二星董事";
                    }
                    if (value == "4") {
                        html = "三星董事";
                    }
                    if (value == "5") {
                        html = "区经理商";
                    }
                    return html;
                },
                status: function (value, row, index) {
                    var html = "";
                    if (value == "0") {
                        html = "待分红";
                    }
                    if (value == "1") {
                        html = "已分红";
                    }
                    return html;
                },
                score: function (value, row, index) {
                    var html = "";
                    if (row.status == 0) {
                        return "";
                    }
                    if (parseInt(value) > 0) {
                        html = value;
                    }
                    else {
                        html = "";
                    }
                    return html;
                },
                percent: function (value, row, index) {
                    var html = "";
                    if (parseInt(value) > 0) {
                        html = value + "%";
                    }
                    else {
                        html = "";
                    }
                    return html;
                },
                month: function (value, row, index) {
                    return value;
                },
                type: function (value, row, index) {
                    var html = "平均";
                    return html;
                },
            }
        },
    };
    return Controller;
});
